package com.yinian.cart.controller;


import com.yinian.cart.entity.CartList;
import com.yinian.cart.service.CartList.CartListService;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController//把方法的返回值返回到页面上
@RequestMapping("/Cart")
public class CartController {
    @Autowired
    private CartListService cartListService;
    @PostMapping("/addCart")
    public String addCart(@RequestBody String json){
        JSONObject jsonObject = new JSONObject(json);
        String productId = (String) jsonObject.get("productId");
        int productCount = (int) jsonObject.get("productCount");
        int userAccount = (int) jsonObject.get("userAccount");
        CartList cartList = cartListService.addNewCartList(productId,productCount,userAccount);
        JSONObject result = new JSONObject();
        if (cartList!=null){
            result.put("status","ok");
        }else{
            result.put("status","no");
        }
        return result.toString();
    }

    @PostMapping("/getAllCartList")
    public String getAllCartList(@RequestBody String count){
        JSONObject jsonObject = new JSONObject(count);
        int userAccount = jsonObject.getInt("userAccount");

        JSONArray jsonArray = cartListService.findAllCartListByUserAccount(userAccount);
        JSONObject result = new JSONObject();
        if (jsonArray!=null){
            result.append("data",jsonArray);
            result.append("status","ok");
        }else{
            result.append("status","no");
        }
        return result.toString();
    }

    @PostMapping("/changeCart")
    public String ChangeCart(@RequestBody String body){
        JSONObject jsonObject = new JSONObject(body);
        String cartId = jsonObject.getString("cartId");
        int productCount = jsonObject.getInt("productCount");
        int userAccount = jsonObject.getInt("userAccount");

        JSONObject jsonObject1 = cartListService.ChangeCart(cartId,productCount,userAccount);
        return jsonObject1.toString();
    }

    @PostMapping("/buyCart")
    public String buyCart(@RequestBody String body){
        JSONArray jsonArray = new JSONArray(body);
        String orderId = cartListService.setOrder(jsonArray);
        JSONObject jsonObject = new JSONObject();
        if (!orderId.equals("")){
            jsonObject.put("status","ok");
        }else{
            jsonObject.put("status","no");
        }
        return jsonObject.toString();
    }
    @PostMapping("/deleteCartList")
    public String deleteCartList(@RequestBody String body){
        JSONObject jsonObject = new JSONObject(body);
        String cartId = jsonObject.getString("cartId");
        int key = cartListService.deleteCartListByCartId(cartId);
        JSONObject jsonObject1 = new JSONObject();
        if (key == 1){
            jsonObject1.put("status","ok");
        }else {
            jsonObject1.put("status","no");
        }
        return jsonObject1.toString();
    }
}
